/*
 * power-ss805x.h
 *
 * The power driver for SS805X.
 *
 * Copyright (C) 2024 Sinh Micro, Inc.
 * Subject to the GNU Public License, version 2.
 *
 * Author: lixiang<lixiang@sinhmicro.com>
 * 
 * Encoding format: GB2312
 * Version: v1.2.2
 * Date: 2024-11-05
 */

#ifndef __HAL_POWER_SS805X_H__
#define __HAL_POWER_SS805X_H__

#include "hal-config.h"
#include "hal-regs.h"

/*
 * In order to optimize the code size, we MUST realize
 * the most of POWER's API using Macro, please realize these APIs as below:
 * 1. Get the DCIN's, VIN's or VKEY's OK STATUS(1, OK; 0, non-OK)
 *    POWER_IS_DCIN_OK()
 *    POWER_IS_VIN_OK()
 *    POWER_IS_VKEY_OK()
 *
 * 2. enable or disable LVR(Low VDD Reset) function
 *    POWER_LVR_ENABLE()
 *    POWER_LVR_DISABLE()
 *
 * 3. make CPU into IDLE mode
 *    POWER_CPU_IDLE()
 * 
 * 4. enable/disable AlwaysTimer wakeup
 *    POWER_ATIMER_WAKEUP_ENABLE()
 *    POWER_ATIMER_WAKEUP_DISABLE() 
 */

#define POWER_IS_DCIN_OK()      (P1WKF & 0x80)  

#define POWER_LVR_ENABLE()      do { LVDCON |= 0x01; } while (0)
#define POWER_LVR_DISABLE()     do { LVDCON &= 0xfe; } while (0)

#define POWER_CPU_IDLE()        do { PCON |= 0x01; } while (0)

#define POWER_ATIMER_WAKEUP_ENABLE()     (ATIMERCON |= (1 << 1)) 
#define POWER_ATIMER_WAKEUP_DISABLE()    (ATIMERCON &= ~(1 << 1))

#define POWER_IS_P03_WAKEUP()         (!!(P0WKF & (1 << 3)))
#define POWER_IS_P07_WAKEUP()         (!!(P0WKF & (1 << 7)))
#define POWER_IS_P10_WAKEUP()         (!!(P1WKF & (1 << 0)))
#define POWER_IS_P11_WAKEUP()         (!!(P1WKF & (1 << 1)))
#define POWER_IS_P12_WAKEUP()         (!!(P1WKF & (1 << 2)))

#define POWER_IS_VIN_WAKEUP()         (!!(P1WKF & (1 << 5)))
#define POWER_IS_ATIMER_WAKEUP()      (!!(P1WKF & (1 << 6)))

#endif
